﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Data.SqlClient;
namespace CpuUsed
{
    public partial class Form1 : Form
    {
        SystemInfo systeminfo = new SystemInfo();
        SqlConnection con = new SqlConnection();
        SqlCommand command = new SqlCommand();
        bool btick = false;
        string constring = "server=172.18.0.41;database=logdb;uid=sa;pwd=comefirstfangrenfu@0";
        public Form1()
        {
            try
            {
                InitializeComponent();
                SqlConnection con = new SqlConnection();
                SqlCommand command = new SqlCommand();
                GetIP Obj = new GetIP();
                string[] IPStrings=Obj.getLocalIP();
                for(int i=0;i<IPStrings.Length;i++){
                    if (IPStrings[i].ToString()!= null)
                        cbIP.Items.Add(IPStrings[i]);
                }
                cbIP.SelectedIndex = cbIP.Items.Count-1;
                con.ConnectionString = constring;
                command = con.CreateCommand();
                command.CommandType = CommandType.Text;
                con.Open();
                command.CommandText = "insert into server(ip,status) select @serverip,0 where not exists (select * from server where ip=@serverip)";
                command.Parameters.Add("@serverip", SqlDbType.VarChar);
                command.Parameters["@serverip"].Value = cbIP.SelectedItem.ToString();
                command.ExecuteNonQuery();
                con.Close();
            }
            catch(Exception e){
                throw e;
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
            btick=!btick;
            timer.Enabled = btick;
            button1.Text=btick==false?"开始":"结束";
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void timer_Tick(object sender, EventArgs e)
        {

            try
            {
                getSystemData();//获取数据
                databaseWrite();//写入数据库
                
             }
            catch(Exception eq){
                throw eq;
            }
        }
        private void getSystemData()
        {
            tbCPU.Text = (Math.Round(systeminfo.CpuLoad, 1)).ToString();
            tbMemory.Text = (Math.Round((100 - 100.0 * systeminfo.MemoryAvailable / systeminfo.PhysicalMemory), 1)).ToString();
            tbConnection.Text = systeminfo.ConnectCount.ToString();
        }
        private void databaseWrite(){
                SqlConnection con = new SqlConnection();
                SqlCommand command = new SqlCommand();
                con.ConnectionString = constring;
                command = con.CreateCommand();
                command.CommandType = CommandType.Text;

                con.Open();
                command.CommandText = "update server set cpu=@cpu,memory=@memory,connect=@connect,date=getdate() where ip=@serverip";
                command.Parameters.Clear();
                command.Parameters.Add("@cpu", SqlDbType.Decimal);
                command.Parameters.Add("@memory", SqlDbType.Decimal);
                command.Parameters.Add("@serverip", SqlDbType.VarChar);
                command.Parameters.Add("@connect", SqlDbType.Int);
               
                command.Parameters["@cpu"].Value = tbCPU.Text;
                command.Parameters["@memory"].Value = tbMemory.Text;
                command.Parameters["@connect"].Value = tbConnection.Text;
                command.Parameters["@serverip"].Value = cbIP.SelectedItem.ToString(); ;
                command.ExecuteNonQuery();
                con.Close();
        }

        
    }
   
}
